package org.scboy.coobi.utils;

import java.io.File;
import java.util.Date;

import org.scboy.coobi.caches.DbTableCache;
import org.scboy.coobi.caches.SqlCache;
import org.scboy.utils.entitys.coobi.DbColumn;
import org.scboy.utils.entitys.coobi.DbIndex;
import org.scboy.utils.entitys.coobi.DbTable;
import org.scboy.utils.entitys.coobi.SQLConfig;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.XmlUtil;

/**
 * 
 * 配置缓存写入文件
 * @author 杨浩
 */
public class CacheWriteFileUtil {
	
	/**
	 * 创建子标签，并赋值
	 * @param parent
	 * @param tagName
	 * @param text
	 * @return
	 */
	private static Element createTag(Element parent,String tagName,Object text) {
		Element tag=parent.getOwnerDocument().createElement(tagName);
		tag.setTextContent(Convert.toStr(text));
		parent.appendChild(tag);
		return tag;
	}
	
	/**
	 * 缓存表写入文件
	 * @param table
	 */
	public static void writeTable(DbTable table,String path) {
		String rootPath=path+File.separator+"tables";
		File root=new File(rootPath);
		if(!root.exists()) {
			root.mkdirs();
		}
		//table文件路径
		String tableFilePath=rootPath+File.separator + table.getName()+".entity.xml";
		File tableFile=new File(tableFilePath);
		if(tableFile.exists()) {
			tableFile.delete();
		}
		Document doc=XmlUtil.createXml();
		
		Element rootTag=doc.createElement("root");

		//创建表名称标签
		createTag(rootTag,"name", table.getName());
		//创建表说明标签
		createTag(rootTag,"comment", table.getComment());
		
		//创建表版本标签
		createTag(rootTag,"version", table.getVersion());
		
		//处理列节点
		Element columns=doc.createElement("columns");
		
		for (DbColumn column : table.getColumns()) {

			Element columnTag= doc.createElement("column");
			columnTag.setAttribute("name", column.getName());
			columnTag.setAttribute("entityName", column.getEntityName());
			columnTag.setAttribute("dbType", column.getDbType());
			columnTag.setAttribute("primary",Convert.toStr(column.isPrimary()));
			columnTag.setAttribute("notNull", Convert.toStr(column.isNotNull()));
			columnTag.setAttribute("length", Convert.toStr(column.getLength()));
			columnTag.setAttribute("comment", column.getComment());
			columns.appendChild(columnTag);
		}
		rootTag.appendChild(columns);
		//处理索引节点
		Element indexs=doc.createElement("indexs");
		for (DbIndex index : table.getIndexs()) {
			Element indexTag=doc.createElement("index");
			indexTag.setAttribute("id", index.getIndexName());
			indexTag.setTextContent(index.getColumns());
			indexs.appendChild(indexTag);
		}
		rootTag.appendChild(indexs);
		
		doc.appendChild(rootTag);
		//保存xml配置文件
		XmlUtil.toFile(doc, tableFilePath);
	}
	
	/**
	 * 缓存SQL写入文件
	 * @param sql
	 */
	public static void writeSql(SQLConfig sql,String path) {
		String rootPath=path+File.separator+"sqls";
		File root=new File(rootPath);
		if(!root.exists()) {
			root.mkdirs();
		}
		//table文件路径
		String sqlFilePath=rootPath + File.separator + sql.getId()+".sql.xml";
		File sqlFile=new File(sqlFilePath);
		if(sqlFile.exists()) {
			sqlFile.delete();
		}
		Document doc=XmlUtil.createXml();
		

		Element rootTag=doc.createElement("root");
		

		Element sqlTag=doc.createElement("sql");
		
		//设置属性
		sqlTag.setAttribute("id", sql.getId());
		sqlTag.setAttribute("name", sql.getName());
		sqlTag.setAttribute("note", sql.getNote());
		sqlTag.setAttribute("version",Convert.toStr(sql.getSqlEntity().getVersion()));
		//设置配置sql
		sqlTag.setTextContent(sql.getSqlEntity().getSql().replaceAll("\r", ""));
		rootTag.appendChild(sqlTag);
		
		doc.appendChild(rootTag);
		
		XmlUtil.toFile(doc, sqlFilePath);
	}
	
	/**
	 * 内存配置缓存为文件
	 */
	public static void cacheToFile() {
		//每天一个固定一个缓存文件，用户操作失误，查找缓存的文件
		String timeDic=DateUtil.format(new Date(), "yyyyMMdd");
		
		String rootPath=JarPathUtil.getLocation()+File.separator+"resource"+File.separator+timeDic;
		for (DbTable table : DbTableCache.getDbTables()) {
			writeTable(table,rootPath);
		}
		
		for (SQLConfig sql : SqlCache.getSqlConfigs()) {
			writeSql(sql,rootPath);
		}
	}
}
